package com.easy.web.service.impl;

import com.easy.web.mapper.ReviewMapper;
import com.easy.web.model.Review;
import com.easy.web.model.ReviewExample;
import com.easy.web.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wang0 on 2017/8/24.
 */
@Service
@Transactional
public class ReviewServiceImpl implements ReviewService{

    @Autowired
    private ReviewMapper reviewMapper;
    /**
     * 查询留言列表
     */
    @Override
    public List<Review> getReviewList() {
        return getReviewListDB();
    }

    /**
     * 新增留言
     *
     * @param review
     */
    @Override
    public List<Review> addReview(Review review) {
        reviewMapper.insert(review);
        return getReviewListDB();
    }

    /**
     * 删除一个留言
     *
     * @param id
     */
    @Override
    public List<Review> delReview(String id) {
        reviewMapper.deleteByPrimaryKey(id);
        return getReviewListDB();
    }

    /**
     * 更新留言信息
     *
     * @param review
     */
    @Override
    public List<Review> updateReview(Review review) {
        reviewMapper.updateByPrimaryKeyWithBLOBs(review);
        return getReviewListDB();
    }

    /**
     * 根据id查询留言
     *
     * @param id
     */
    @Override
    public Review queryReviewById(String id) {
        return reviewMapper.selectByPrimaryKey(id);
    }


    public List<Review> getReviewListDB() {
        ReviewExample reviewExample=new ReviewExample();
        reviewExample.setOrderByClause("ABS(floor)");
        return reviewMapper.selectByExampleWithBLOBs(reviewExample);
    }

    /**
     * 根据楼层查询留言信息
     *
     * @param floor
     * @return
     */
    @Override
    public Review queryReviewByFloor(String floor) {
        ReviewExample reviewExample=new ReviewExample();
        reviewExample.createCriteria().andFloorEqualTo(floor);
        List<Review> reviewList=reviewMapper.selectByExample(reviewExample);
        if(reviewList.size()>0){
           return  reviewList.get(0);
        }
        return null;
    }
}
